package com.ly.nethub.netty.tcp

import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer

internal class NettyTcpChannelInitializerHandler(private val netHub: TcpNetHub) :
    ChannelInitializer<Channel>() {

    override fun initChannel(ch: Channel?) {
        ch ?: return
        val encoders = netHub.netHubOptions.encoders.map {
            it()
        }.toTypedArray()
        val decoders = netHub.netHubOptions.decoders.map {
            it()
        }.toTypedArray()
        ch.pipeline()
            .addLast(*encoders)
            .addLast(*decoders)
            .addLast(
                NettyTcpReadHandler::class.java.simpleName,
                NettyTcpReadHandler(netHub.mTaskManager, netHub)
            )
    }
}